package com.yt.sys.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.sys.models.bo.SysUserSaveBO;
import com.yt.sys.models.dto.LoginInfoDTO;
import com.yt.sys.models.dto.SysUserDTO;
import com.yt.sys.models.dto.SysUserRoleDTO;
import com.yt.sys.models.service.ILoginInfoService;
import com.yt.sys.models.service.ISysUserRoleService;
import com.yt.sys.models.service.ISysUserService;
import com.yt.sys.models.vo.SysPermissionVO;
import com.yt.sys.models.vo.SysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统用户信息控制器
 *
 * @author zhou
 * @Date 2025-09-03 14:25:55
 */

@RestController
@RequestMapping("/api/sysUser")
@Slf4j
@Api(value = "SysUserApiController", tags = "系统用户信息Api接口")
public class SysUserApiController {

    @DubboReference
    private ISysUserService sysUserService;
    @DubboReference
    private ISysUserRoleService sysuserRoleService;
    @DubboReference
    private ILoginInfoService loginInfoService;

    @PostMapping("/SysUserInfoAdd")
    @ApiOperation(value = "系统用户新增", notes = "系统用户新增")

    public Result<SysUserDTO> SysUserInfoAdd(@RequestBody SysUserSaveBO bo) {
        if (bo == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        SysUserDTO dto = sysUserService.saveSysUser(bo);


        return new Result<>(dto);
    }

    @PostMapping("/SysUserInfoDelete")
    @ApiOperation(value = "系统用户删除", notes = "系统用户删除")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    @Transactional
    public Result<Boolean> userInfoDelete(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        //1.删除系统用户
        boolean flag = sysUserService.deleteById(id);
        //2.删除系统用户角色表
        Wrapper<SysUserRoleDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", id);
        sysuserRoleService.delete(wrapper);
        //3.删除登录表
        Wrapper<LoginInfoDTO> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("user_id", id);
        loginInfoService.delete(wrapper1);

        return new Result<>(flag);
    }

    @PostMapping("/SysUserInfoUpdate")
    @ApiOperation(value = "系统用户修改", notes = "系统用户修改")
    @Transactional
    public Result<Boolean> SysUserInfoUpdate(@RequestBody SysUserSaveBO bo) {
        if (bo.getId() == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = sysUserService.updateSysUser(bo);
        return new Result<>(flag);
    }

    @PostMapping("/querySysUser")
    @ApiOperation(value = "查询系统用户", notes = "查询系统用户")

    public Result querySysUser() {
        List<SysUserVO> sysUserVOS = sysUserService.querySysUsers();

        return new Result<>(sysUserVOS);
    }

    @PostMapping("/querySysUserPermission")
    @ApiOperation(value = "查询系统用户权限", notes = "查询系统用户权限")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result querySysUserPermission(@RequestParam Long id) {
        List<SysPermissionVO> vos = sysUserService.queryPermission(id);
        return new Result<>(vos);
    }

    @PostMapping("/queryEmployee")
    @ApiOperation(value = "查询员工信息", notes = "查询员工信息")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<List<SysUserVO>> queryEmployee() {
        List<SysUserVO> vos = sysUserService.queryEmp();
        return new Result<>(vos);
    }
}

